<?php
header("Content-Type: text/html; charset=iso-8859-1");
header("Content-Type: text/html; charset=iso-8859-1");
require_once('include.php');
$limit = (!empty($_REQUEST['nb'])) ? $_REQUEST['nb'] : 1;
$start = (!empty($_REQUEST['start'])) ? $_REQUEST['start'] : 1;
$where = (!empty($_REQUEST['id'])) ? "and id = '".$_REQUEST['id']."'" : "";
$hide = (!empty($_REQUEST['hide'])) ? $_REQUEST['hide'] : 0;
$unique = (!empty($_REQUEST['unique'])) ? $_REQUEST['unique'] : 0;
######### Print only corps ########
if(!empty($_REQUEST['corps'])){
	$id = $_REQUEST['corps'];
	$info = new info($id);
	if(substr($info->corps,0,strlen("historique")) == 'historique'){
		$historique_id = trim(substr($info->corps,strlen("historique")));
		$historique = new historique($historique_id);
		// echo prepare_info($historique->corps);
		echo "<div class = 'historique'><h2>$historique->titre</h2><br />".prepare_info($historique->corps,'historique')."</div><p class = 'bold'><a href = 'http://www.vbcll.ch/historique.php?article=$historique_id&search=1'>&rarr;&nbsp;retrouvez plus d'infos de la saison $historique->periode.</a></p>";
	}
	else echo prepare_info($info->corps);
}
#### Print only title #######
elseif($hide){
	$id = $_REQUEST['id'];
	$info = new info($id);
	echo "<p class = 'liste_titre_info'>\n";
	echo "<span class = 'liste_date_info'>".format_date($info->datetime)."</span>$info->titre</p>\n";
}
######## Print all ##############
else{
	$q = "select * from infos where valide = 1 $where order by datetime desc";
	$r = ExecRequete($q);
	$i = 0;
	$nb = 0;
	if($start < mysql_num_rows($r)) echo "<a href = 'javascript:liste_infos(".($start+1).",\"accueil\",0)'><span class = 'suivant'>suivant &darr;</span></a>";
	if(!$unique) echo "<!--!>";
	if($start > 1) echo "<a href = 'javascript:liste_infos(".($start-1).",\"accueil\",0)'><img src = 'images/icon_precedent.png' width = '53' height = '16' alt = 'precedent' /></a>";
	else echo "&nbsp;";
	$lastweek = mktime(0, 0, 0, date("m"), date("d")-7,   date("Y"));
	if(!$unique) echo "<!--!>";
	while($s = LigneSuivante($r)){
		$membre = new membre($s->membre_id);
		$auteur = CapitalizeLastName(strtolower($membre->prenom." ".$membre->nom));
		if(!$s->small) $i++;
		if($i >= $start || ($i == 0 && $s->small) || $limit == 1){
			$q1 = "select count(*) as nb from commentaires_info where info_id = $s->id";
			$r1 = ExecRequete($q1);
			$s1 = LigneSuivante($r1);
			$texte_commentaire = ($s1->nb < 2) ? "$s1->nb commentaire" : "$s1->nb commentaires";
			$onclick = (basename($_SERVER['HTTP_REFERER']) == 'infos.php') ? "onclick = 'show_info($s->id)' style = 'cursor: pointer'" : "";
			if(!$s->small) $nb++;
			if($limit == 'accueil'){
				if($nb > 2 && strtotime($s->datetime) < $lastweek) break;
			}
			elseif($nb >= $limit && $limit != 1) break;
			if($s->small){
				if($nb < 2 || strtotime($s->datetime) > $lastweek){
					$titre = $s->titre."<span class = 'float_right'>".(format_date($s->datetime))."</span>";
					$corps = prepare_info($s->corps);
					echo box($titre,$corps,690,1);
				}
			}
			else{
				echo "<div class = 'info'>\n";
				if($auteur == 'Vbcll ' && substr($s->corps,0,strlen("historique")) == 'historique'){
					$historique = new historique(str_replace("historique ","",$s->corps));
					$date_diff = dateDifference($historique->date,date("Y-m-d"));
					echo "<div class = 'info_title' $onclick><span class = 'historique'>C'&eacute;tait il y a ";
					if($date_diff[0]) echo $date_diff[0]." an".(($date_diff[0]>1)?"s ":" ");
					if($date_diff[1]) echo $date_diff[1]." mois ";
					if($date_diff[2]) echo "et ".$date_diff[2]." jour".(($date_diff[2]>1)?"s ":" ");
					echo "</span></div>\n";

					echo "<div class = 'info_auteur'>Les archives du VBCLL<span class = 'info_date'>".format_date($historique->date)."</span></div>\n";
					// $s->corps = "<div class = 'historique'><h2>$historique->titre</h2><br />".$historique->corps."</div>";
					$s->corps = "<div class = 'historique'><h2>$historique->titre</h2><br />".prepare_info($historique->corps,'historique')."</div><p class = 'bold'><a href = 'http://www.vbcll.ch/historique.php?article=$historique_id&search=1'>&rarr;&nbsp;retrouvez plus d'infos de la saison $historique->periode.</a></p>";

					$cat = "historique";
				}
				else{
					echo "<div class = 'info_title' $onclick>".$s->titre."</div>\n";
					if($auteur == 'Vbcll ') $auteur = "tous les membres";
					echo "<div class = 'info_auteur'>par $auteur<span class = 'info_date'>".(format_date($s->datetime))."</span></div>\n";
					$cat = 'infos';
				}
				if($limit == 'accueil'){
					if(strlen($s->corps) > 300 && $s->titre != TITRE_TWEET){
						// $corps = ereg("^([.]{100,})?\n",$s->corps,$regs) ? $regs[1]."<br /><a href = 'javascript:show_corps_info($s->id)'>[suite...]</a>" : $s->corps;
						if((preg_match_all("/(.*)\n/",$s->corps,$regs))){
							$corps = "";
							$length = 0;
							foreach($regs as $array){
								foreach($array as $cell){
									if($length+strlen($cell) <= 300){
										$corps .= $cell;
										$length += strlen($cell);
									}
									else break(2);
								}
							}
							if(!$length) $corps = $regs[0][0];
							$corps .= "<br /><a href = 'javascript:show_corps_info($s->id)' class = 'suite'>[suite...]</a>";
						}
						else $corps = $s->corps;
					}
					else $corps = $s->corps;
					// $pattern = utf8_encode("/(.{10,})\n/i");
					// $corps = preg_match($pattern,$s->corps,$regs);
				}
				else $corps = $s->corps;
				echo "<div class = 'info_corps' id = 'info_$s->id'>".(prepare_info($corps,$cat))."</div>\n";
				echo "<div class = 'info_commentaire'><a href = 'infos.php?id=$s->id#commentaires'>$texte_commentaire</a></div>\n";
				echo "</div>\n";
			}
		}
	}
}
?>
